package cn.yuanqiao.archive.danganzhengli.utils.impl;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.danganzhengli.utils.OfdAgent;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Component
@ConditionalOnProperty(value= "YQ.OFD.VERSION" ,havingValue = "OCR" )
public class OCRAgent implements OfdAgent {
    protected static final Logger logger = LoggerFactory.getLogger(OCRAgent.class);
    //@Value("${HD_SUWELL_AGENT_URL}")
    //private String HD_SUWELL_AGENT_URL;
    @Autowired
    private ITenantSysConfigService sysConfigService;
    @Override
    public void officesToOfd(List<File> fileList, String outPath) throws Exception {
        String SINGLETOMULTPATH = sysConfigService.selectConfigByKey("SINGLETOMULTPATH");
        //if(!StringUtils.isNotEmpty(SINGLETOMULTPATH) || "null".equalsIgnoreCase(SINGLETOMULTPATH)){
        //    throw new ServiceException("ocr插件路径未配置");
        //}
        logger.info("OCRAgent:officesToOfd=outPath1:"+outPath);
        outPath=outPath.substring(0, outPath.lastIndexOf(File.separator)-3)+"pdf"+File.separator+outPath.substring(outPath.lastIndexOf(File.separator)+1,outPath.length()).substring(0,outPath.substring(outPath.lastIndexOf(File.separator)+1,outPath.length()).lastIndexOf("."))+".PDF";
        logger.info("OCRAgent:officesToOfd=outPath2:"+outPath);
        if(!new File(outPath).exists()) {
            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            // ocr 插件路径
            String ocrPath = SINGLETOMULTPATH;
            try{
                ProcessBuilder pb = new ProcessBuilder();
                File d = new File(ocrPath.substring(0, ocrPath.lastIndexOf(File.separator)));
                pb.directory(d);
                String path = fileList.get(0).getPath();
                pb.command(ocrPath,path,outPath,"300");
                Process p=pb.start();
                p.waitFor();
            }catch(Exception e){
                e.printStackTrace();
                throw e;
            }
        }
    }

    @Override
    public void imageToOFD(List<File> filesList, String outPath) throws Exception {
        String SINGLETOMULTPATH = sysConfigService.selectConfigByKey("SINGLETOMULTPATH");
        if(!StringUtils.isNotEmpty(SINGLETOMULTPATH) || "null".equalsIgnoreCase(SINGLETOMULTPATH)){
            throw new ServiceException("ocr插件路径未配置");
        }
        logger.info("OCRAgent:imageToOFD=outPath:"+outPath);
        outPath=outPath.substring(0, outPath.lastIndexOf(File.separator)-3)+"pdf"+File.separator+outPath.substring(outPath.lastIndexOf(File.separator)+1,outPath.length()).substring(0,outPath.substring(outPath.lastIndexOf(File.separator)+1,outPath.length()).lastIndexOf("."))+".PDF";
        if(!new File(outPath).exists()) {

            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            // ocr 插件路径
            String ocrPath = SINGLETOMULTPATH;
            try{
                ProcessBuilder pb = new ProcessBuilder();
                File d = new File(ocrPath.substring(0, ocrPath.lastIndexOf(File.separator)));
                pb.directory(d);
                String path = filesList.get(0).getPath();
                pb.command(ocrPath,path,outPath,"300");
                Process p=pb.start();
                p.waitFor();
            }catch(Exception e){
                e.printStackTrace();
                throw e;
            }
        }
    }
}
